﻿using ControleRepresentante.Logic.Models;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Impl;

namespace ControleRepresentante.Logic.Repositories
{
    internal class ConvertedDetachedCriteria : DetachedCriteria
    {
        public ConvertedDetachedCriteria(ICriteria criteria)
            : base((CriteriaImpl)criteria, criteria)
        {
            var impl = (CriteriaImpl)criteria;
            impl.Session = null;
        }
    }

    internal static class CriteriaHelper
    {
        public static DetachedCriteria ToDetachedCriteria(this ICriteria criteria)
        {
            return new ConvertedDetachedCriteria((ICriteria) criteria.Clone());
        }

        public static DetachedCriteria ToDetachedCriteriaProjectId(this ICriteria criteria)
        {
            return criteria.ToDetachedCriteria().SetProjection(Projections.Property<ModelBase>(x => x.Id));
        }
    }
}
